package pms.Biz.impl;

import pms.Biz.OrderBiz;
import pms.dao.OrderDao;
import pms.dao.impl.OrderDaoImpl;
import pms.entity.Medicine;
import pms.entity.Order;
import pms.entity.Supplier;

import java.time.LocalDate;
import java.util.List;

public class OrderBizImpl implements OrderBiz {
    OrderDao orderDao=new OrderDaoImpl();
    @Override
    public boolean appendMedicineOrder(Supplier supplier, LocalDate order_date,Integer order_number,Integer order_amount,Integer order_status) {
        Order order=null;
        List<Medicine> medicineList=orderDao.selectMedicineByNecessity();
        for(Medicine medicine:medicineList){
            if(orderDao.selectSupplierById(supplier).getSupplier_mainMedicine()==medicine.getMedicine_id()){
                order=new Order();
                order.setOrder_amount(order_amount);
                order.setOrder_date(order_date);
                order.setOrder_medicine(medicine);
                order.setOrder_status(order_status);
                order.setOrder_supplier(supplier);
                return true;
            }
        }
        return false;
    }

    @Override
    public Supplier querySupplierInformationById(Integer supplier_id) {
        return  orderDao.selectSupplierById(supplier_id);
    }

    @Override
    public boolean updateSupplierInformationById(Integer supplier_id
            ,String supplier_name
            ,Integer supplier_qct
            ,String supplier_tel
            ,String supplier_status
            ,Integer supplier_mainMedicine
            ,Integer supplier_price) {
        Supplier supplier=null;
        supplier.setSupplier_price(supplier_price);
        supplier.setSupplier_mainMedicine(supplier_mainMedicine);
        supplier.setSupplier_qct(supplier_qct);
        supplier.setSupplier_name(supplier_name);
        supplier.setSupplier_tel(supplier_tel);
        supplier.setSupplier_status(supplier_status);
        List<Supplier> supplierList=orderDao.selectAllSupplier();
        for(Supplier supplier1:supplierList){
            if(supplier1.getSupplier_id()==supplier_id){
                orderDao.updateSupplierById(supplier);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Medicine> queryMedicineOrder() {
        return orderDao.selectMedicineByNecessity();
    }

    @Override
    public boolean shortageOfMedicineAlertBYID(Integer medicine_id) {
        Medicine medicine=orderDao.selectMedicineById(medicine_id);
        if(orderDao.judgeIfNeedToPurchaseExtraMedicine(medicine)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Supplier electTheBestSupplierByMedicineId(Integer medicine_id) {
        return orderDao.compareSupplierToGetBestPrice(medicine_id);
    }

    @Override
    public boolean deleteQuestionSupplierById(Integer supplier_id) {
        if(orderDao.deleteProblemSupplierById(orderDao.selectSupplierById(supplier_id))){
            return true;
        }else{
            return false;
        }
    }

}
